/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.planificacion;

import Presenter.PedidoPresenter;
import java.util.logging.Level;
import java.util.logging.Logger;
import Model.Pedido;
import Model.EnvioPedido;
import Presenter.EnvioPedidoPresenter;
import Principals.Main;
import java.util.*;
import Principals.Utils;
import Principals.Validadores;
import javax.swing.JOptionPane;

/**
 *
 * @author sebastian
 */
public class Frm_MonitorearPedidos extends javax.swing.JDesktopPane {

    Validadores val = new Validadores();

    /**
     * Creates new form Frm_PrincipalPlanificacion
     */
    public Frm_MonitorearPedidos() {
        Main.usrSesion.refresh();
        initComponents();
        EnvioPedidoPresenter.actualizarEnvioPedido();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        pnlPrincipal = new javax.swing.JPanel();
        pnlDatosPedido = new javax.swing.JPanel();
        lblEstadoPedido = new javax.swing.JLabel();
        lblHoraEstimada = new javax.swing.JLabel();
        lblVehiculo = new javax.swing.JLabel();
        lblConductor = new javax.swing.JLabel();
        txtEstadoPedido = new javax.swing.JTextField();
        txtHoraEstimada = new javax.swing.JTextField();
        txtConductor = new javax.swing.JTextField();
        txtVehiculo = new javax.swing.JTextField();
        pnlBusquedaPedido = new javax.swing.JPanel();
        lblCodigoPedido = new javax.swing.JLabel();
        txtCodigoPedido = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();

        pnlDatosPedido.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Pedido"));

        lblEstadoPedido.setText("Estado del Pedido");

        lblHoraEstimada.setText("Hora Estimada de Entrega");

        lblVehiculo.setText("Vehículo Asociado");

        lblConductor.setText("Conductor Asociado");

        txtEstadoPedido.setEditable(false);
        txtEstadoPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEstadoPedidoActionPerformed(evt);
            }
        });

        txtHoraEstimada.setEditable(false);

        txtConductor.setEditable(false);

        txtVehiculo.setEditable(false);

        org.jdesktop.layout.GroupLayout pnlDatosPedidoLayout = new org.jdesktop.layout.GroupLayout(pnlDatosPedido);
        pnlDatosPedido.setLayout(pnlDatosPedidoLayout);
        pnlDatosPedidoLayout.setHorizontalGroup(
            pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosPedidoLayout.createSequentialGroup()
                .add(84, 84, 84)
                .add(pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblHoraEstimada)
                    .add(lblEstadoPedido)
                    .add(lblConductor)
                    .add(lblVehiculo))
                .add(24, 24, 24)
                .add(pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(txtHoraEstimada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtEstadoPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtConductor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtVehiculo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 191, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        pnlDatosPedidoLayout.setVerticalGroup(
            pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblEstadoPedido)
                    .add(txtEstadoPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblHoraEstimada)
                    .add(txtHoraEstimada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblConductor)
                    .add(txtConductor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(pnlDatosPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblVehiculo)
                    .add(txtVehiculo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pnlBusquedaPedido.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda de Pedidos"));

        lblCodigoPedido.setText("Código del Pedido");

        txtCodigoPedido.setToolTipText("ingrese el codigo del pedido a buscar");
        txtCodigoPedido.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCodigoPedidoKeyTyped(evt);
            }
        });

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout pnlBusquedaPedidoLayout = new org.jdesktop.layout.GroupLayout(pnlBusquedaPedido);
        pnlBusquedaPedido.setLayout(pnlBusquedaPedidoLayout);
        pnlBusquedaPedidoLayout.setHorizontalGroup(
            pnlBusquedaPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlBusquedaPedidoLayout.createSequentialGroup()
                .add(128, 128, 128)
                .add(lblCodigoPedido)
                .add(48, 48, 48)
                .add(txtCodigoPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 121, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(123, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlBusquedaPedidoLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(btnBuscar)
                .add(217, 217, 217))
        );
        pnlBusquedaPedidoLayout.setVerticalGroup(
            pnlBusquedaPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlBusquedaPedidoLayout.createSequentialGroup()
                .add(9, 9, 9)
                .add(pnlBusquedaPedidoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblCodigoPedido)
                    .add(txtCodigoPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(btnBuscar)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout pnlPrincipalLayout = new org.jdesktop.layout.GroupLayout(pnlPrincipal);
        pnlPrincipal.setLayout(pnlPrincipalLayout);
        pnlPrincipalLayout.setHorizontalGroup(
            pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlBusquedaPedido, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlDatosPedido, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlPrincipalLayout.setVerticalGroup(
            pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlBusquedaPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlDatosPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtEstadoPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEstadoPedidoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEstadoPedidoActionPerformed

    public void BuscarPedido(int codPedigo) {
        Pedido objPedido;
        objPedido = PedidoPresenter.buscarPedidoPorCodigo(codPedigo);
        if (objPedido == null) {
            JOptionPane.showMessageDialog(null, "No existen un pedido asociado al codigo: " + txtCodigoPedido.getText());
            txtCodigoPedido.setText("");
            txtEstadoPedido.setText("");
            txtConductor.setText("");
            txtHoraEstimada.setText("");
            txtVehiculo.setText("");
        } else {
            actualizarPedido(objPedido);
            llenarDatos(objPedido);
        }

    }

    public void actualizarPedido(Pedido objPedido) {
        if (objPedido.getFechaEntrega() != null) {
            if (new Date().after(objPedido.getFechaEntrega())) {
                objPedido.setEstado(2);
                PedidoPresenter.modificarPedido(objPedido);
            }
        }
    }

    public void llenarDatos(Pedido objPedido) {
        String estadoPedido = "";
        String horaEstimada = "";
        String conductor = "";
        String vehiculo = "";
        if (objPedido.getEstado() == 0) {
            estadoPedido = "Pendiente";
        }
        if (objPedido.getEstado() == 1) {
            estadoPedido = "En camino";
        }
        if (objPedido.getEstado() == 2) {
            estadoPedido = "Entregado";
        }
        txtEstadoPedido.setText(estadoPedido);
        if (objPedido.getFechaEntrega() != null && !estadoPedido.equalsIgnoreCase("Pendiente")) {
            txtHoraEstimada.setText(Utils.formatDateWithHours(objPedido.getFechaEntrega()));
        }

        List<EnvioPedido> lstEnvioPedido = new ArrayList<EnvioPedido>();
        lstEnvioPedido = EnvioPedidoPresenter.buscarEnvioPedido();
        List<Pedido> lstPedidos = new ArrayList<Pedido>();
        //buscamos el pedido dentro de envio pedido para encontrar el vehiculo y chofer
        for (int i = 0; i < lstEnvioPedido.size(); i++) {
            lstPedidos = new ArrayList(lstEnvioPedido.get(i).getPedidos());
            for (int j = 0; j < lstPedidos.size(); j++) {
                if (objPedido.getIdPedido() == lstPedidos.get(j).getIdPedido()) {
                    conductor = lstEnvioPedido.get(i).getEmpleado().getNombres() + " " + lstEnvioPedido.get(i).getEmpleado().getApellidoPaterno();
                    vehiculo = lstEnvioPedido.get(i).getVehiculo().getPlaca();
                    break;
                }
            }
        }

        txtConductor.setText(conductor);
        txtVehiculo.setText(vehiculo);

    }

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        if (txtCodigoPedido.getText().equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(null, "Ingrese un codigo a buscar");
        } else {
            int codigoPedido = Integer.parseInt(txtCodigoPedido.getText());
            BuscarPedido(codigoPedido);
        }


    }//GEN-LAST:event_btnBuscarActionPerformed

    private void txtCodigoPedidoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoPedidoKeyTyped
        // TODO add your handling code here:
        val.validaSoloNumeros(evt);
        val.validaMaxSize(evt, 6, txtCodigoPedido.getText());
    }//GEN-LAST:event_txtCodigoPedidoKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JLabel lblCodigoPedido;
    private javax.swing.JLabel lblConductor;
    private javax.swing.JLabel lblEstadoPedido;
    private javax.swing.JLabel lblHoraEstimada;
    private javax.swing.JLabel lblVehiculo;
    private javax.swing.JPanel pnlBusquedaPedido;
    private javax.swing.JPanel pnlDatosPedido;
    private javax.swing.JPanel pnlPrincipal;
    private javax.swing.JTextField txtCodigoPedido;
    private javax.swing.JTextField txtConductor;
    private javax.swing.JTextField txtEstadoPedido;
    private javax.swing.JTextField txtHoraEstimada;
    private javax.swing.JTextField txtVehiculo;
    // End of variables declaration//GEN-END:variables
}
